package com.heima.day602_List集合;


import java.util.ArrayList;
import java.util.Scanner;


public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    /*数组类型为什么类型，只能存入什么类型的对象
    当数组的元素类型是一个类名时，未来数组中要存储的是该类的对象
    booleam add(E e)将指定的元素添加到此列表的尾部
    .add(索引位置,元素)
              Integer 包装int类型存储整数类型*/


    public static void main(String[] args) {


        //2创建集合对象
        ArrayList<Student> stuLi = new ArrayList<>();
        Student stu1 = getStudent(1);
        Student stu2 = getStudent(2);
        Student stu3 = getStudent(3);

        //5将学生对象添加到集合中
        stuLi.add(stu1);
        stuLi.add(stu2);
        stuLi.add(stu3);

        //6遍历集合
        for (int i = 0; i < stuLi.size(); i++) {

            Student stu = stuLi.get(i);
            //打印的是集合的地址
            System.out.println(stu.getName() + "   " + stu.getAge());   //打印Student集合的元素
        }


    }

    public static Student getStudent(int count) {
        //3键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第"+count+"位学生姓名：");
        String name = sc.next();
        System.out.println("请输入第"+count+"位学生年龄：");
        int age = sc.nextInt();
        //4创建学生对象，将录入的数据赋值给学生对象的成员变量
        Student stu = new Student(name, age);
        return stu;
    }

}
